<template>
  <el-dialog :visible="visible"
             :before-close="handleClose"
             width="800px"
             title="乙方"
             append-to-body>
    <ReplaceComponentLayer>
      <template slot="component">
        <dn-object-select-card :valueKey="valueKey"
                               :filterKey="filterKey"
                               :columns="tableColumn"
                               :placeholder="placeholder"
                               :disableKey="disableKey"
                               :disableTip="disableTip"
                               :remote-search="loadCarriers"
                               v-on="$listeners"
                               v-bind="$attrs"
                               @submit="handleSubmit"
                               @cancel="handleClose" />
      </template>
    </ReplaceComponentLayer>
  </el-dialog>
</template>

<script>
import { basic } from "@service";
import ReplaceComponentLayer from '@packages/basic/ReplaceComponentLayer'
export default {
  name: "SelectCarrier",
  props: {
    visible: Boolean,
  },
  components: { ReplaceComponentLayer },
  data () {
    return {
      valueKey: "carrierId",
      filterKey: "name",
      placeholder: "承运方名称",
      tableColumn: [
        {
          label: "承运方名称",
          name: "name"
        },
        {
          label: "承运方联系人",
          name: "contactName"
        },
        {
          label: "联系电话",
          name: "contactMobile"
        },
        {
          label: "承运方邮箱",
          name: "contactEmail"
        }
      ]
    };
  },
  computed: {
    mergedFilterData () {
      return {
        type: [1],
        extraType: 0,
        status: 1,
      };
    },
    disableKey () {
      return "hasCertAuthority"
    },
    disableTip () {
      return "承运方没有证书权限，不能操作选择"
    }
  },
  methods: {
    loadCarriers (filterData) {
      const filterValue = {
        ...this.mergedFilterData, ...filterData
      }
      return basic.loadCarriers(filterValue);
    },
    handleClose () {
      this.$emit("update:visible", false);
    },
    handleSubmit (action) {
      if (!action?.[this.disableKey]) {
        return false;
      }
      if (action && action.relation) {
        this.$emit("selectCarrier", action);
        this.$emit("update:visible", false);
      } else {
        this.$message.warning("请选择乙方");
      }
    }
  }
};
</script>
